﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using WWLauncher.Util;

namespace WWLauncher.ViewModel
{
    public class PowerWindowViewModel : INotifyPropertyChanged
    {

        Win32PowSchemasWrapper wrapper = new Win32PowSchemasWrapper();
        public PowerWindowViewModel()
        {
            schemas = wrapper.GetCurrentSchemas();
            var activeGuid = wrapper.GetActiveGuid();
            foreach (var schema in schemas)
            {
                if (schema.Guid == activeGuid)
                {
                    schema.IsActive = true;
                    activeSchema = schema;
                }
            }
        }

        private Guid[] defaultGuids = {
            new("8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c"),
            new("381b4222-f694-41f0-9685-ff5bb260df2e"),
            new("a1841308-3541-4fab-bc81-f71556f20b4a") };

        private string name;
        private Guid guid;
        private List<PowerSchema> schemas;
        private PowerSchema activeSchema;


        public string Name
        {
            get => name;
            set { name = value; OnPropertyChanged("Name"); }
        }
        public List<PowerSchema> Schemas
        {
            get => schemas;
            set { schemas = value; OnPropertyChanged("Schemas"); }
        }
        public PowerSchema ActiveSchema
        {
            get => activeSchema;
            set
            {
                activeSchema = value; 
                wrapper.SetActiveGuid(value.Guid);
                OnPropertyChanged("ActiveSchema");
            }
        }
        public Guid Guid
        {
            get => guid;
            set { guid = value; OnPropertyChanged("Guid"); }
        }


        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public void Refresh()
        {
            wrapper.UpdateSchemas();
        }
    }
}
